package com.quran.viewcontrollers;

import java.util.List;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.bappi.utils.AlertUtils;
import com.bappi.utils.Constants;
import com.custom.tab.AbstractTabRootManager;
import com.custom.tab.AbstractViewController;
import com.markupartist.android.widget.ActionBar;
import com.tos.quran.R;

public class Tab1Controller extends AbstractViewController {

	private ActionBar actionBar;

	private List<String[]> suras;
	private LanguageListAdapter suraListAdapter;
	private ListView listView;

	public Tab1Controller(AbstractTabRootManager tabRootManager) {
		super(tabRootManager, R.layout.select_sura);
		try {
			actionBar = (ActionBar) findViewById(R.id.actionbar);
			actionBar.setTitle(R.string.app_name);
			// actionBar.setVisibility(View.GONE);
			listView = (ListView) findViewById(R.id.list_view);
			new AsyncTask<String, String, String>() {
				private AlertUtils.SpinnerProgressDialog spinnerProgressDialog;

				@Override
				protected void onPreExecute() {
					try {
						spinnerProgressDialog = AlertUtils.getProgressDialog(
								getActivity(),
								getString(R.string.dialog_default_title),
								getString(R.string.please_wait));
						spinnerProgressDialog.show();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}

				@Override
				protected String doInBackground(String... arg0) {
					try {
						suras = getActivity().getDatabaseAccessor().getSuras();

					} catch (Exception e) {
						e.printStackTrace();
					}
					return null;
				}

				protected void onPostExecute(String unused) {
					try {
						suraListAdapter = new LanguageListAdapter();
						listView.setAdapter(suraListAdapter);
						spinnerProgressDialog.dismiss();
//						progressDialog.dismiss();
						listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

							public void onItemClick(AdapterView<?> av, View v,
									int position, long arg) {
								showSura(position);
							}
						});
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}.execute();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void showSura(int index) {
		try {
			String[] namesAra = new String[suras.size()];
			String[] namesEng = new String[suras.size()];
			String[] meaning = new String[suras.size()];
			for (int i = 0; i < suras.size(); i++) {
				namesAra[i] = suras.get(i)[1];
				namesEng[i] = suras.get(i)[2];
				meaning[i] = suras.get(i)[3];
			}

			Bundle bundle = new Bundle();
			bundle.putInt(Constants.KEY_SURA_ID,
					Integer.parseInt(suras.get(index)[0]));
			bundle.putStringArray(Constants.KEY_SURA_NAMES_ARA, namesAra);
			bundle.putStringArray(Constants.KEY_SURA_NAMES_ENG, namesEng);
			bundle.putStringArray(Constants.KEY_SURA_MEANING, meaning);
			pushViewController(new ShowVersesController(getTabRootManager(),
					bundle));

			// Intent intent = new Intent(getParent(),ShowVerses.class);
			// intent.putExtra(Constants.KEY_SURA_ID,
			// Integer.parseInt(suras.get(index)[0]));
			// intent.putExtra(Constants.KEY_SURA_NAMES_ARA, namesAra);
			// intent.putExtra(Constants.KEY_SURA_NAMES_ENG, namesEng);
			// intent.putExtra(Constants.KEY_SURA_MEANING, meaning);
			// pushIntnet(intent);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public class LanguageListAdapter extends BaseAdapter {

		private LayoutInflater mInflater;

		public LanguageListAdapter() {
			// mInflater = (LayoutInflater) getParent().getSystemService(
			// Context.LAYOUT_INFLATER_SERVICE);
			mInflater = (LayoutInflater) getActivity().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
		}

		public int getCount() {
			return suras.size();
		}

		public Object getItem(int arg0) {
			Object ret = null;
			if (suras != null && suras.size() > arg0) {
				ret = suras.get(arg0);
			}
			return ret;
		}

		public long getItemId(int position) {
			return 0;
		}

		public View getView(final int position, View convertView,
				ViewGroup parent) {
			try {
				ViewHolder holder;
				if (convertView == null) {
					convertView = mInflater.inflate(R.layout.sura_list_content,
							null);
					holder = new ViewHolder();
					holder.mainContainer = convertView
							.findViewById(R.id.main_container);
					holder.suraIndex = (TextView) convertView
							.findViewById(R.id.view_sura_index);
					holder.suraNameAra = (TextView) convertView
							.findViewById(R.id.view_sura_name_ara);
					holder.suraNameEng = (TextView) convertView
							.findViewById(R.id.view_sura_name_eng);
					holder.suraNameMeaning = (TextView) convertView
							.findViewById(R.id.view_meaning);
					// holder.suraNameAra.setTypeface(Utils.getArabicFont(getParent()));
					convertView.setTag(holder);
				} else {
					holder = (ViewHolder) convertView.getTag();
				}
				holder.suraIndex
						.setText(String.format("%02d.", (position + 1)));
				holder.suraNameAra.setText("(" + suras.get(position)[1] + ")");
				holder.suraNameEng.setText(suras.get(position)[2]);
				holder.suraNameMeaning.setText(suras.get(position)[3]);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return convertView;
		}

		class ViewHolder {
			View mainContainer;
			TextView suraIndex;
			TextView suraNameAra;
			TextView suraNameEng;
			TextView suraNameMeaning;
		}
	}
}
